﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace Utils.Weixin.Pay.Result
{
    public class RefundResult
    {
        public string Mchid { get; set; }
        public string RefundId { get; set; }
        public string TransactionId { get; set; }

        public string OutTradeNo { get; set; }


        public string OutRefundNo { get; set; }


        public decimal RefundAmount { get; set; }


        public decimal TotalAmount { get; set; }

        /// <summary>
        /// 退款状态，枚举值：
        /// PROCESSING :处理中
        /// SUCCESS：退款成功
        /// CLOSED：退款关闭
        /// ABNORMAL：退款异常，退款到银行发现用户的卡作废或者冻结了，导致原路退款银行卡失败，可前往【商户平台—>交易中心】，手动处理此笔退款
        /// </summary>
        public string RefundStatus { get; set; }
        /// <summary>
        /// 成功时间
        /// </summary>
        public DateTime? SuccessTime { get; set; }
        /// <summary>
        /// 退款专户
        /// </summary>
        public string UserReceivedAccount { get; set; }

        internal static RefundResult Create(JObject jobj)
        {
            return new RefundResult()
            {
                RefundAmount = ((int)jobj["amount"]["refund"]) / 100m,
                TotalAmount = ((int)jobj["amount"]["total"]) / 100m,
                TransactionId = jobj["transaction_id"].ToString(),
                RefundId = jobj["refund_id"].ToString(),
                OutTradeNo = jobj["out_trade_no"].ToString(),
                OutRefundNo = jobj["out_refund_no"].ToString(),
                UserReceivedAccount = jobj["user_received_account"].ToString(),
                RefundStatus = jobj["status"]?.ToString() ?? jobj["refund_status"]?.ToString(),
                SuccessTime = jobj["success_time"]?.Value<DateTime>(),
            };
        }

    }
}
